﻿namespace CSharpPro._1027;

using System;

/// <summary>
/// 歌曲类，用于表示歌曲的基本信息和相关操作
/// </summary>
public class Song
{
    // 私有字段
    private string _title;       // 歌曲标题
    private string _artist;      // 艺术家/歌手
    private TimeSpan _duration;  // 歌曲时长
    private string _album;       // 所属专辑
    private int _releaseYear;    // 发行年份
    private string _genre;       // 音乐类型

    // 公共属性（带数据验证）
    public string Title
    {
        get => _title;
        set => _title = !string.IsNullOrWhiteSpace(value) 
            ? value 
            : throw new ArgumentException("歌曲标题不能为空或空白", nameof(value));
    }

    public string Artist
    {
        get => _artist;
        set => _artist = !string.IsNullOrWhiteSpace(value) 
            ? value 
            : throw new ArgumentException("艺术家名称不能为空或空白", nameof(value));
    }

    public TimeSpan Duration
    {
        get => _duration;
        set
        {
            if (value <= TimeSpan.Zero)
                throw new ArgumentOutOfRangeException(nameof(value), "歌曲时长必须大于0");
            _duration = value;
        }
    }

    public string Album
    {
        get => _album;
        set => _album = value ?? string.Empty; // 允许空专辑名（默认为空字符串）
    }

    public int ReleaseYear
    {
        get => _releaseYear;
        set
        {
            int currentYear = DateTime.Now.Year;
            if (value < 1800 || value > currentYear + 1) // 允许明年发行的歌曲（预发布）
                throw new ArgumentOutOfRangeException(nameof(value), 
                    $"发行年份必须在1800到{currentYear + 1}之间");
            _releaseYear = value;
        }
    }

    public string Genre
    {
        get => _genre;
        set => _genre = value ?? string.Empty;
    }

    // 构造函数
    public Song(string title, string artist, TimeSpan duration, int releaseYear)
    {
        Title = title;
        Artist = artist;
        Duration = duration;
        ReleaseYear = releaseYear;
        // 可选属性默认初始化
        Album = string.Empty;
        Genre = string.Empty;
    }

    // 带全部参数的构造函数
    public Song(string title, string artist, TimeSpan duration, 
                string album, int releaseYear, string genre)
        : this(title, artist, duration, releaseYear)
    {
        Album = album;
        Genre = genre;
    }

    // 方法：播放歌曲
    public void Play()
    {
        Console.WriteLine($"正在播放：《{Title}》 - {Artist}");
        Console.WriteLine($"时长：{Duration:mm\\:ss} | 专辑：{Album} | 发行年份：{ReleaseYear}");
    }

    // 方法：显示歌曲详情
    public string GetDetails()
    {
        return $"歌曲信息：\n" +
               $"标题：{Title}\n" +
               $"艺术家：{Artist}\n" +
               $"专辑：{Album}\n" +
               $"时长：{Duration:mm\\:ss}\n" +
               $"发行年份：{ReleaseYear}\n" +
               $"类型：{Genre}";
    }

    // 重写ToString方法
    public override string ToString()
    {
        return $"{Title} - {Artist} ({ReleaseYear})";
    }
}